#include <cereal/archives/binary.hpp>
#include <cereal/archives/json.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/types/unordered_map.hpp>
#include <array_example.hpp>
#include <gtest/gtest.h>

TEST(TestArray, ArrayInStruct) {
/*  DSL Def:
    struct SimpleArrays {
        Array<float32_t, 3> x;
        Array<Map<int, Array<float32_t,3>>, 2> y;
    }
*/
    std::array<float, 3> v1{1.0f, 2.0f, 3.0f};
    std::array<float, 3> v2{11.0f, 12.0f, 13.0f};
    std::array<float, 3> v3{111.0f, 112.0f, 113.0f};

    std::map<int, std::array<float,3>> map1;
    map1[1] = v1;
    std::map<int, std::array<float,3>> map2;
    map2[2] = v2;
    map2[3] = v3;
    std::array<std::map<int, std::array<float,3>>, 2> maps{map1,map2};
    SimpleArrays foo{.x{v1}, .y{maps}};
    EXPECT_FLOAT_EQ(foo.x[0], 1.0f);
    EXPECT_FLOAT_EQ(foo.x[1], 2.0f);
    EXPECT_FLOAT_EQ(foo.x[2], 3.0f);
    EXPECT_FLOAT_EQ(foo.y[1].at(2)[0], 11.0f);
}


TEST(TestArray, ArrayInClass) {
    Triangle triangle;
    std::array<Color,3> colors{Color{0,1,2},Color{0,11,12},Color{0,21,22}};
    std::array<Vec2f,3> vertices{Vec2f{0,0},Vec2f{0,1},Vec2f{11,0}};
    triangle.set_colors(colors);
    triangle.set_vertices(vertices);

    EXPECT_EQ(triangle.get_colors()[0].green, 1);
    EXPECT_EQ(triangle.get_colors()[2].green, 21);
    EXPECT_FLOAT_EQ(triangle.get_vertices()[2].x, 11);

    std::stringstream ss;
    {
        cereal::JSONOutputArchive archive(ss);
        archive(triangle);
    }
    Triangle t2;
    ss.seekg(0);
    {
        cereal::JSONInputArchive archive(ss);
        archive(t2);
    }
    EXPECT_EQ(triangle.get_colors()[2].green, t2.get_colors()[2].green);
}